/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 中科软支付平台
 * 创建日期 : 2016年11月23日
 * 修改历史 : 
 *     1. [2016年11月23日]创建文件 by liuyujia
 */
package com.sinosoft.sinopay.service.customer.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinosoft.sinocloud.common.biz.service.BizNumGenerateCoreService;
import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.util.DateUtil;
import com.sinosoft.sinopay.api.common.config.ENUM_PAY_BIZNO_TYPE;
import com.sinosoft.sinopay.api.common.dto.pay.PayAccountInfoDTO;
import com.sinosoft.sinopay.customer.dto.CustomerInfoDTO;
import com.sinosoft.sinopay.service.customer.biz.service.CustomerBankAcountCoreService;
import com.sinosoft.sinopay.service.customer.biz.service.CustomerInfoCoreService;
import com.sinosoft.sinopay.service.customer.generated.mapper.PayCustomerBankcardMapper;
import com.sinosoft.sinopay.service.customer.generated.mapper.PayCustomerMainMapper;
import com.sinosoft.sinopay.service.customer.generated.model.PayCustomerBankcard;
import com.sinosoft.sinopay.service.customer.generated.model.PayCustomerBankcardExample;
import com.sinosoft.sinopay.service.customer.generated.model.PayCustomerMain;
import com.sinosoft.sinopay.service.customer.generated.model.PayCustomerMainExample;

import org.springframework.stereotype.Service;

/**  
 * 【客户信息服务接口实现类】
 * @author liuyujia
 * @date 2016年11月23日 下午2:42:55 
 * @version V1.0  
 */
@Service
public class CustomerInfoCoreServiceImpl extends SinoBaseService implements CustomerInfoCoreService{

	@Autowired
	private PayCustomerMainMapper payCustomerMainMapper;
	@Autowired
	private PayCustomerBankcardMapper payCustomerBankcardMapper;
	@Autowired
	private BizNumGenerateCoreService bizNumGeneratorService;
	
	@Override
	public void addNewCustomer(CustomerInfoDTO cCustomerInfo) {
		Date current = DateUtil.getCurrentDate();
		PayCustomerMain tCustomer = new PayCustomerMain();
			String customerNo = bizNumGeneratorService.generateBizNum(ENUM_PAY_BIZNO_TYPE.CUSTOMER.code());
			tCustomer.setCustomerNo(customerNo);
//			tCustomer.setCustomerNo("123456789");
			tCustomer.setName(cCustomerInfo.getCustomerName());
			tCustomer.setGender(cCustomerInfo.getGender());
			tCustomer.setBirthday(cCustomerInfo.getBirthday());
			tCustomer.setCardNo(cCustomerInfo.getIdNo());
			tCustomer.setCardType(cCustomerInfo.getIdType());
			tCustomer.setMobile(cCustomerInfo.getMobile());
			tCustomer.setCreateDate(current);
			payCustomerMainMapper.insert(tCustomer);
			cCustomerInfo.setCustomerId(tCustomer.getSid());
			cCustomerInfo.setCustomerCode(customerNo);
	}

	@Override
	public void modifyCustomerInfo(CustomerInfoDTO cCustomerInfo) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Integer checkCustomerExist(CustomerInfoDTO cCustomerInfo) {
		//根据五项基本要素来判断客户是否重复
		PayCustomerMainExample tCustomerExam = new PayCustomerMainExample();
		if(cCustomerInfo.getIdType()!=null && cCustomerInfo.getIdType().equals("1")){
			tCustomerExam.createCriteria()
						.andBirthdayEqualTo(cCustomerInfo.getBirthday())
						.andGenderEqualTo(cCustomerInfo.getGender())
						.andCardTypeEqualTo(cCustomerInfo.getIdType())
						.andCardNoEqualTo(cCustomerInfo.getIdNo())
						.andNameEqualTo(cCustomerInfo.getCustomerName());
					List<PayCustomerMain> result = payCustomerMainMapper.selectByExample(tCustomerExam);
					if(result!=null && result.size()!=0){
						return result.get(0).getSid();
					}
					return 0;
		}
		else{
			tCustomerExam.createCriteria()
						.andCardTypeEqualTo(cCustomerInfo.getIdType())
						.andCardNoEqualTo(cCustomerInfo.getIdNo())
						.andNameEqualTo(cCustomerInfo.getCustomerName());
					List<PayCustomerMain> result = payCustomerMainMapper.selectByExample(tCustomerExam);
					if(result!=null && result.size()!=0){
						return result.get(0).getSid();
					}
					return 0;
		}
				
	}
}
